﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using THSLib;using THSERP.BaseProvider;
namespace THSERP.Modules.ReturnSupplier
{
    public partial class guiReceiptSuppliers : THSERPScreen
    {
        public guiReceiptSuppliers()
        {
            InitializeComponent();
            IntializeDataToSearchControls();
            IntializeDataToRepository();
        }

        public void IntializeDataToSearchControls()
        {
            //inital Supplier
            DataSet dsSuppliers = new APSuppliersController().GetAllObjects();
            if (dsSuppliers != null)
            {
                if (dsSuppliers.Tables.Count > 0)
                    fld_lkeAPSupplierID.Properties.DataSource = dsSuppliers.Tables[0];
            }
            //search datetime
            fld_dteSearchFromDate.EditValue = THSUtil.GetYearBeginDate();
            fld_dteSearchToDate.EditValue = THSUtil.GetYearEndDate();
        }

        public void IntializeDataToRepository()
        {
            //receipt
            DataSet dsReceipts = new ICReceiptsController().GetAllObjects();
            if (dsReceipts != null)
            {
                if (dsReceipts.Tables.Count > 0)
                    rplkeICReceiptID.DataSource = dsReceipts.Tables[0];
            }
            DataSet dsProducts = new ICProductsController().GetAllObjects();
            if(dsProducts!=null)
            {
                if (dsProducts.Tables.Count > 0)
                    rplkeProduct.DataSource = dsProducts.Tables[0];
            }
        }

        private void fld_btnSearch_Click(object sender, EventArgs e)
        {
            GetReceiptBySupplierAndFromdateTodate();
        }

        #region--Utilities--
        public void GetReceiptBySupplierAndFromdateTodate()
        {
            ICReceiptsController objReceiptsController = new ICReceiptsController();
            String query = GenerateSearchQuery(Receipt.ReceiptEntities.strICReceiptObjectName);
            DataSet ds = objReceiptsController.GetDataSet(query);
            THSList<ICReceiptsInfo> lstReceipts = new THSList<ICReceiptsInfo>();
            lstReceipts.InitTHSList(null, String.Empty, Receipt.ReceiptEntities.strICReceiptObjectName);
            lstReceipts.Invalidate(ds);
            fld_dgcICReceipts.DataSource = lstReceipts;
        }
        #endregion

        public bool SelectReceipt()
        {
            //Generate supplier return from receipt
            if (fld_dgvICReceipts.FocusedRowHandle >= 0)
            {
                ReturnSupplierEntities entity = (ReturnSupplierEntities)((BaseModuleERP)Module).CurrentModuleEntity;
                ICReceiptsInfo objICReceiptsInfo = (ICReceiptsInfo)fld_dgvICReceipts.GetRow(fld_dgvICReceipts.FocusedRowHandle);
                APReturnSuppliersInfo objReturnSuppliersInfo = (APReturnSuppliersInfo)entity.MainObject;
                objReturnSuppliersInfo.APReturnSupplierReference = objICReceiptsInfo.ICReceiptNo;
                objReturnSuppliersInfo.FK_APSupplierID = Convert.ToInt32(fld_lkeAPSupplierID.EditValue);
                ((ReturnSupplierModule)Module).SetDefaultSupplierContactInformation();
                entity.UpdateMainObjectBindingSource();

                GenerateReferenceReceiptItems(objICReceiptsInfo.ICReceiptID);

                return true;
            }
            return false;
        }

        private void fld_btnAdd_Click(object sender, EventArgs e)
        {
            if (SelectReceipt())
                this.DialogResult = DialogResult.OK;
            else
                this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void fld_dgcICReceipts_DoubleClick(object sender, EventArgs e)
        {
            if (SelectReceipt())
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        public void GenerateReferenceReceiptItems(int receiptID)
        {
            ReturnSupplierEntities entity = (ReturnSupplierEntities)((BaseModuleERP)Module).CurrentModuleEntity;
            ICReceiptItemsController objICReceiptItemsController = new ICReceiptItemsController();
            DataSet dsReceiptItems = objICReceiptItemsController.GetAllDataByForeignColumn("FK_ICReceiptID", receiptID);
            entity.ICReferenceReceiptItemsList.Invalidate(dsReceiptItems);
        }

        private void fld_btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void guiReceiptSuppliers_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.DialogResult != DialogResult.OK)
                ((BaseModuleERP)Module).ActionCancel();
        }
    }
}